package com.huazai.yangge.completable.future;

import java.util.concurrent.*;

/**
 * @author 华仔
 * @date 2024/2/21
 */
public class CompletableFutureFastDemo {

    // 比较线程哪个快
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        final CompletableFuture<String> play1 = CompletableFuture.supplyAsync(() -> {
            System.out.println("111 = ");
            try {
                TimeUnit.MILLISECONDS.sleep(100);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return "play 111";
        });

        final CompletableFuture<String> play2 = CompletableFuture.supplyAsync(() -> {
            System.out.println("222 = ");
            try {
                TimeUnit.MILLISECONDS.sleep(200);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return "play 222";
        });

        final CompletableFuture<String> stringCompletableFuture = play1.applyToEither(play2, s -> "winner is " + s);
        System.out.println(Thread.currentThread().getName() + "\t" + "======" + stringCompletableFuture.get());
    }
}

// thenRun 任务A执行完执行任务B， 并且B不需要A的结果
// thenAccept 任务A执行完执行任务B， B需要A的结果， 但是B无返回值
// thenApply 任务A执行完执行任务B， B需要A的结果 同时任务B有返回值






